package com.yequan.hbb.adapter;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yequan.hbb.R;
import com.yequan.hbb.entries.BaseBean;
import com.yequan.hbb.entries.FocusBean;
import com.yequan.hbb.entries.RecentBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gwf on 2016/10/14.
 */

public class HPCAdapter extends RecyclerView.Adapter<HPCAdapter.ViewHolder>{
    private Context context;
    private List<BaseBean> list;
    private int[] layouts;
    private FragmentManager manager;

    public HPCAdapter(Context context, FragmentManager manager) {
        this.context = context;
        this.manager = manager;
        list = new ArrayList<>();
        layouts = new int[]{R.layout.focus_image_item, R.layout.recent};
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 0) {
           view = LayoutInflater.from(context).inflate(R.layout.focus_image_item, parent, false);
        } else if (viewType == 1) {
            view = LayoutInflater.from(context).inflate(R.layout.recent, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if (list.size() != 0){
            BaseBean bean = list.get(position);
            if (bean instanceof FocusBean){
                List<FocusBean.DataBean.CategoryBean.ItemBean> list = ((FocusBean) bean).getData().getCategory().get(0).getItem();
                HPCPagerAdapter adapter = new HPCPagerAdapter(context, list);
                holder.pager.setAdapter(adapter);
                for (FocusBean.DataBean.CategoryBean.ItemBean bean1 : list) {
                    ImageView imageView = new ImageView(context);
                    imageView.setImageResource(R.drawable.point);
                    int v = (int) context.getResources().getDimension(R.dimen.point);
                    imageView.setPadding(v,v,v,v);
                    holder.fPoints.addView(imageView);
                }
                holder.pager.addOnPageChangeListener(holder);
                holder.pager.setCurrentItem(1);
                holder.fPoint.post(new Runnable() {
                    @Override
                    public void run() {
                        ViewCompat.setTranslationX(holder.fPoint, holder.fPoints.getLeft());
                    }
                });
                holder.handler.obtainMessage(0, 1, 0).sendToTarget();
            } else if (bean instanceof RecentBean) {
                holder.recentPager.setAdapter(new UpDateAdapter(manager));
                holder.tab.setupWithViewPager(holder.recentPager);
            }
        }


    }

    @Override
    public int getItemCount() {
        return layouts.length;
    }

    @Override
    public int getItemViewType(int position) {
        if (list.size() != 0) {
            if (list.get(position) instanceof FocusBean){
                return 0;
            } else if (list.get(position) instanceof RecentBean){
                return 1;
            }
        }
        return super.getItemViewType(position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements Handler.Callback, ViewPager.OnPageChangeListener {

        private final ViewPager pager;
        private final ImageView fPoint;
        private final TabLayout tab;
        private final ViewPager recentPager;
        private Handler handler = new Handler(this);
        private final LinearLayout fPoints;

        public ViewHolder(View itemView) {
            super(itemView);
            pager = ((ViewPager) itemView.findViewById(R.id.focus_pager));
            fPoint = ((ImageView) itemView.findViewById(R.id.focus_point));
            fPoints = ((LinearLayout) itemView.findViewById(R.id.focus_points));
            tab = ((TabLayout) itemView.findViewById(R.id.recent_tab));
            recentPager = ((ViewPager) itemView.findViewById(R.id.recent_pager));
        }
        @Override

        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int arg1 = msg.arg1;
                    if (arg1 > pager.getAdapter().getCount() - 2) {
                        arg1 = 1;
                    }
                    pager.setCurrentItem(arg1, arg1 != 1);
                    Message message = handler.obtainMessage(0, ++arg1, 0);
                    handler.sendMessageDelayed(message, 3000);
                    break;
            }
            return true;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float value = fPoints.getLeft() + (position - 1 + positionOffset) * fPoint.getWidth();
            value = Math.max(value, fPoints.getLeft());
            value = Math.min(value, fPoints.getRight() - fPoint.getWidth());
            ViewCompat.setTranslationX(fPoint, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = pager.getAdapter();
            if (position == 0) {
                pager.setCurrentItem(adapter.getCount() - 2, false);
            } else if (position == adapter.getCount() - 1) {
                pager.setCurrentItem(1, false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }
    public void add(BaseBean bb) {
        list.add(bb);
        notifyDataSetChanged();
    }

}
